﻿using ERPU8WebApi.Until;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace ERPU8WebApi.Dal
{
    //生产制造
    public class DALProduction
    {
        SqlHelper s = new SqlHelper();

        //根据MoId查MoCode
        public string GetMoCode(string MoId)
        {
            string ccode = "";
            string sql = string.Format(@"SELECT mocode FROM mom_order where MoId='{0}' ", MoId);
            DataTable dt = new DataTable();
            dt = s.ExecuteDataSetSql(sql, 0).Tables[0];
            if (dt.Rows.Count > 0)
            {
                ccode = dt.Rows[0]["mocode"].ToString();
            }
            return ccode;
        }
        //更新生产订单制单人
        public int UpdateMomCreateUser(string mocode, string createUser)
        {
            //string cmaker = GetUserName(createUser);
            int i = 0;
            string sql = string.Format(@"UPDATE mom_order SET CreateUser='{0}' where MoCode='{1}' ", createUser, mocode);
            i = s.ExecuteNonQuery(sql, 0);
            if (i > 0)
            {
                return 1;
            }
            return i;
        }

        //根据生产订单号和存货编码查料单  取生产订单创建时间大于2021年1月1日的
        public DataTable GetMoallocate(string mocode, string cinvcode)
        {
            string sql = string.Format(@"SELECT m.InvCode,i.cInvCode,i.cInvStd,i.cInvName FROM mom_moallocate m 
                                         INNER JOIN dbo.Inventory i 
                                         ON m.InvCode=i.cInvCode
                                         WHERE m.MoDId IN 
                                         (SELECT modid FROM mom_orderdetail 
                                         WHERE moid IN 
                                         (SELECT moid FROM dbo.mom_order WHERE MoCode='{0}' AND YEAR(CreateDate)>='2021') AND mom_orderdetail.InvCode='{1}') AND m.Qty>0"
                                          , mocode, cinvcode);
            DataTable dt = new DataTable();
            dt = s.ExecuteDataSetSql(sql, 0).Tables[0];
            return dt;
        }
    }
}